<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">

       <!-- 靠右的按钮 -->
       <PageTools :show-after="true">
        <template v-slot:after>
            <el-button type="success" @click="addMenus" size="small" >
             <i class="el-icon-edit" style="color: #fff;"></i>
            添加菜单</el-button>
        </template>
       </PageTools>
       <!-- 表格 -->
       <el-table border :data="list" row-key="id" style="margin-top:5px"
        default-expand-all
        :tree-props="{children: 'childs'}" >
          <el-table-column label="标题" align="center" prop="title" style="width:100%"/>
          <el-table-column label="权限点代码" align="center" prop="code" style="width:100%"/>
          <el-table-column label="操作" align="center" style="width:100%">
          <!-- 按钮 -->
            <template v-slot="{row}">
              <el-button type="primary" icon="el-icon-edit" circle @click="editMenus(row)"/>
              <el-button type="danger" icon="el-icon-delete" circle @click="deleMenus(row)"/>
            </template>
          </el-table-column>
       </el-table>
      </el-card>
    </div>
    <!-- 弹层 -->
    <el-dialog :visible="isShowDia" title="创建菜单" @close="btnChancel" width="600px">
      <!-- 表单 -->
      <el-form label-width="150px" :model="formData" ref="diaForm" :rules="rules">
        <!-- 单选按钮 -->
         <el-form-item label="权限组名称" prop="is_point">
          <el-radio-group v-model="formData.is_point">
            <el-radio :label="false" :disabled="disRad">菜单</el-radio>
            <!--  @change="diaPoint()"权限点添加disabled -->
            <el-radio :label="true" :disabled="disRad">权限点</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 下拉 -->
         <el-form-item label="权限组名称" prop="pid">
          <el-select  placeholder="请选择"
          v-model="formData.pid"
          ref="selectPid" style="width:300px">
            <el-option
            :value="formData.pid"
            :label="label||formData.title"
            style="height: auto"
           >
              <el-tree :data="list" :props="defaultProps" @node-click="handleNodeClick" default-expand-all ></el-tree>
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 输入 -->
        <el-form-item label="权限代码" prop="code">
          <el-input v-model="formData.code" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="权限标题" prop="title">
          <el-input v-model="formData.title" style="width:300px"></el-input>
        </el-form-item>
      </el-form>
       <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnChancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>

import { list, add, update, remove, detail } from '@/api/base/menus.js'
import PageTools from '@/components/PageTools'
export default {
  name: 'treeTableDemo',
  components: {
    PageTools
  },
  data () {
    return {
      list: [], // 接收数据
      isShowDia: false,
      formData: {
        title: '', // 权限名称
        code: '', // 权限代码
        is_point: '', // 是否是权限点
        pid: ''// 父类id
      },
      defaultProps: {
        children: 'childs',
        label: 'title'
      },
      label: '',
      rules: {
        is_point: [
          { required: true, message: '请选择权限', trigger: 'change' }
        ],
        pid: [
          { required: true, message: '请选择名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限代码', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入权限标题', trigger: 'blur' }
        ]
      },
      disRad: false// 编辑给radio禁用
    }
  },
  watch:{

  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data } = await list()
      // 将数据处理，将points改成childs
      this.list = data.map(item => {
        const obj = {}
        obj.id = item.id
        obj.pid = item.pid
        obj.title = item.title
        obj.code = item.code
        if (item.childs) {
          obj.childs = item.childs.map(i => {
            return {
              id: i.id,
              pid: i.pid,
              code: i.code,
              title: i.title,
              childs: i.points
            }
          })
        }
        return obj
      })
    },
    // 确定按钮
    async btnOK () {
      try {
        // 验证表单
        await this.$refs.diaForm.validate()
        // 如果有id就是编辑
        if (this.formData.id) {
          // 更新数据
          const res=await update(this.formData)
          console.log(res)
        } else {
          // 没有id就是添加
          await add(this.formData)
        }
        // 提示
        this.$message.success('新增成功')
        this.getList()// 重新渲染
        this.isShowDia = false// 关闭弹窗
      } catch (error) {
        console.log(error)
      }
    },
    // 取消按钮
    btnChancel () {
      this.formData = {
        title: '', // 权限名称
        code: '', // 权限代码
        is_point: '', // 是否是菜单
        pid: ''// 父类id
      }
      this.label = ''
      this.$refs.diaForm.resetFields()// 重置表单
      this.isShowDia = false// 关闭弹窗
    },
    // 添加菜单
    addMenus () {
      this.isShowDia = true
      this.disRad = false
    },
    // 编辑菜单
    async editMenus (row) {
      this.isShowDia = true
      const { data } = await detail(row)
      this.formData = data
      this.disRad = true
      // this.label=data.title
    },
    // 删除菜单
    async deleMenus (row) {
      try {
        await this.$confirm('你确定删除')
        await remove(row)
        this.getList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 节点被点击的回调
    handleNodeClick (data) {
      console.log(data)
      this.formData.pid = data.id
      this.label = data.title
      this.$refs.selectPid.blur()
    }
    // 点击权限点按钮，将list数据加上disabled
    // diaPoint(){
    //     this.list.forEach(item=>{
    //       if(item.childs){
    //         item.disabled=true
    //       }
    //     })
    // }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

</style>
